<?php
	require_once('environment.php');
	$temp->addSubMenu("Videos", "videos.php");
	$temp->addSubMenu("Photos", "photos.php");
	if($acc->isLogged()) {
		$temp->addSubMenu("Moderate Videos", "videos.php?admin=mod");
		$temp->addSubMenu("Moderate Photos", "photos.php?admin=mod");
		$temp->addSubMenu("Change Media Page", "media.php?admin=change");
	}
	
	$admin = fixArr($_GET, 'admin');
	$videos = new Video($acc, $db);
	
	function throwError($where, $what) {
		print change_site("videos.php?admin={$where}&error={$what}");
		die();
	}
	
	switch($admin) {
		default:
			$temp->setTab(3, 0);
			$temp->currentPage("Home >> Media >> Videos");
			$temp->generate();
			$videos->listVideos();
			break;
		case("mod"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3, 2);
			$temp->currentPage("Home >> Media >> Videos >> Moderate Videos");
			$temp->generate();
			print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n";
			print "<a href=\"videos.php?admin=add\">Add new video</a><br />\n";
			$sql = $db->Execute("SELECT * FROM `monster`.`videos` ORDER BY `AutoInc` ASC");
			if($sql->RecordCount() > 0) {
				while($row = $sql->FetchRow()) {
					print "<br />\n<b>{$row['Name']}</b><br />\n";
					print "<a href=\"videos.php?admin=change&id={$row['AutoInc']}\">Modify</a> | <a href=\"videos.php?admin=delete&id={$row['AutoInc']}\">Delete</a>";
					print "<br />\n";
				}
			}
			break;
		case("change"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3,2);
			$id = fixArr($_GET, "id");
			if(!$id) die(change_site("videos.php?admin=mod"));
			if(!($videoInfo = $videos->getVideoByID($id))) throwError("mod", "A video with this ID does not exist.");
			$temp->currentPage("Home >> Media >> Videos >> Moderate Videos >> Change {$videoInfo['Name']}");
			$temp->generate();
			if(!fixArr($_POST, "name")) {
				print "<form action=\"\" method=\"POST\">\n";
				print "<label>Name <input type=\"text\" name=\"name\" value=\"{$videoInfo['Name']}\" /></label><br />\n";
				print "<label>Link <input type=\"text\" name=\"link\" value=\"{$videoInfo['Link']}\" /></label><br />\n";
				print "<input type=\"submit\" value=\"Change\" /> <input type=\"reset\" value=\"Reset\" />\n";
				print "</form>\n";
			}else{
				if(!($name = fixArr($_POST, "name"))) throwError("change&id={$id}", "You need to fill out all fields.");
				if(!($link = fixArr($_POST, "link"))) throwError("change&id={$id}", "You need to fill out all fields.");
				$videos->changeVideo($id, $name, $link);
				print "The video has been changed successfully.";
			}
			break;
		case("delete"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3,2);
			$id = fixArr($_GET, "id");
			if(!$id) die(change_site("videos.php?admin=mod"));
			if(!($videoInfo = $videos->getVideoByID($id))) throwError("mod", "A video with this ID does not exist.");
			$temp->currentPage("Home >> Media >> Videos >> Moderate Videos >> Delete {$videoInfo['Name']}");
			$temp->generate();
			if(fixArr($_GET, "conf") !== "true") {
				print "Are you SURE you want to delete {$videoInfo['Name']}? This cannot be undone.<br />\n<br />\n";
				print "<a href=\"videos.php?admin=delete&id={$id}&conf=true\">Yes, delete it forever.</a><br />\n<br />\n";
				print "<a href=\"videos.php?admin=mod\">No, don't delete it!</a>\n";
			}else{
				$videos->removeVideo($id);
				print "The video has been deleted successfully.\n";
			}
			break;
		case("add"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3,2);
			$temp->currentPage("Home >> Media >> Videos >> Moderate Videos >> Add Video");
			$temp->generate();
			if(!fixArr($_POST, "name")) {
				print "Please fill out ALL the fields. To enter a video, please upload a video to YouTube, and then enter the entire URL link in the \"link\" text field.";
				print "<br />\n<br />\n";
				print "<form action=\"\" method=\"POST\">\n";
				print "<label>Video Name <input type=\"text\" name=\"name\" /></label><br />\n";
				print "<label>Link <input type=\"text\" name=\"link\" /></label><br />\n";
				print "<input type=\"submit\" value=\"Add Video\" /> <input type=\"reset\" value=\"Reset\" />\n";
				print "</form>\n";
			}else{
				$name = fixArr($_POST, "name");
				$link = fixArr($_POST, "link");
				if(!$link) throwError("add", "You must enter a URL link to the YouTube video.");
				$videos->addVideo($name, $link);
				print "The video has been added successfully.";
			}
			break;
	}
?>